<template>
  <el-row class="good-item" >
    <el-col>
      <el-card :body-style="{ padding: 0 }" @click.native="productDetail()">
        <div class="good-img">
          <a v-if="goods.goods_Bigimg">
            <img alt="" :src="'http://127.0.0.1:8000/' + goods.goods_Bigimg">
          </a>
        </div>
        <h6 class="good-title">{{ goods.goods_name }}</h6>
        <h3>{{ goods.goods_describe }}</h3>
        <div class="good-price pr">
          <div class="ds">
              <el-button style="margin: 0 5px" type="default" size="medium" @click.stop="productDetail()">查看详情</el-button>
              <!-- 这里要加状态控制 -->
              <el-button style="margin: 0 5px" type="primary" size="medium" :disabled=" goods.goods_num <= 0 " @click.stop="addCart(goods.id)">加入购物车</el-button>
          </div>
          <p>
            <span style="font-size:14px">￥</span>
            {{ Number(goods.goods_price).toFixed(2) }}
          </p>
        </div>
      </el-card>
    </el-col>
  </el-row>
</template>

<script>
import { mapState, mapMutations } from 'vuex'

export default {
  props:['goods'],
  computed: {
    ...mapState(['login', 'addCartSuccess'])
  },
  created() {
  },
  methods: {
    ...mapMutations(['ADDCART']),
    productDetail() {
      this.$router.push( `/goodsDetail/${this.goods.id}`)
    },
    async addCart(goods_id, goods_number = 1 ) {
      if(!this.login) {
        return this.$router.push('/login')
      }
      await this.ADDCART({goods_id, goods_number})

      if(!this.addCartSuccess) {
        return this.$notify.error({
          title: '警告',
          message: '添加商品到购物车失败',
          duration: 1500
        })
      }
      this.$notify.success({
          title: '成功',
          message: '成功添加商品到购物车',
          duration: 1500
        })
    },
  }
}
</script>

<style lang="scss" scoped>
@import "../assets/style/mixin";
@import "../assets/style/theme";
.good-item {
  position: relative;
  background: #fff;
  width: 25%;
  transition: all .5s;
  height: 380px;
  &:hover {
    transform: translateY(-3px);
    box-shadow: 1px 1px 20px #999;
    .good-price p {
      display: none;
    }
    .ds {
      display: flex;
    }
  }
  .ds {
    @extend %block-center;
    width: 100%;
    display: none;
  }

  .good-img {
    img {
      margin: 50px auto 10px;
      @include wh(206px);
      display: block;
    }
  }
  .good-price {
    margin: 15px 0;
    height: 30px;
    text-align: center;
    line-height: 30px;
    color: #d44d44;
    font-family: Arial;
    font-size: 18px;
    font-weight: 700;
  }
  .good-title {
    height: 18px;
    line-height: 1.2;
    font-size: 16px;
    color: #424242;
    margin: 0 auto;
    padding: 0 14px;
    text-align: center;
    overflow: hidden;
  }
  h3 {
    text-align: center;
    line-height: 1.2;
    font-size: 12px;
    color: #d0d0d0;
    padding: 10px;
  }

}
</style>